<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
  <script class="testbody" type="application/javascript">
'use strict';

// here we call the identity provider directly
function getIdentityAssertion(fpArray) {
  var Cu = SpecialPowers.Cu;
  var rtcid = Cu.import('resource://gre/modules/media/IdpSandbox.jsm');
  var sandbox = new rtcid.IdpSandbox('example.com', 'idp.js', window);
  return sandbox.start()
    .then(idp => SpecialPowers.wrap(idp)
                   .generateAssertion(JSON.stringify({ fingerprint: fpArray }),
                                      'https://example.com'))
    .then(assertion => {
      assertion = SpecialPowers.wrap(assertion);
      var assertionString = btoa(JSON.stringify(assertion));
      sandbox.stop();
      return assertionString;
    });
}

// This takes a real fingerprint and makes some extra bad ones.
function makeFingerprints(algo, digest) {
  var fingerprints = [];
  fingerprints.push({ algorithm: algo, digest: digest });
  for (var i = 0; i < 3; ++i) {
    fingerprints.push({
      algorithm: algo,
      digest: digest.replace(/:./g, ':' + i.toString(16))
    });
  }
  return fingerprints;
}

var fingerprintRegex = /^a=fingerprint:(\S+) (\S+)/m;
var identityRegex = /^a=identity:(\S+)/m;

function fingerprintSdp(fingerprints) {
  return fingerprints.map(fp => 'a=fInGeRpRiNt:' + fp.algorithm +
                                ' ' + fp.digest + '\n').join('');
}

// Firefox only uses a single fingerprint.
// That doesn't mean we have it create SDP that describes two.
// This function synthesizes that SDP and tries to set it.
function testMultipleFingerprints() {
  // this one fails setRemoteDescription if the identity is not good
  var pcStrict = new RTCPeerConnection({ peerIdentity: 'someone@example.com'});
  // this one will be manually tweaked to have two fingerprints
  var pcDouble = new RTCPeerConnection({});

  var offer, match, fingerprints;

  var fail = msg =>
      (e => ok(false, 'error in ' + msg + ': ' +
               (e.message ? (e.message + '\n' + e.stack) : e)));

  navigator.mediaDevices.getUserMedia({ audio: true, fake: true })
    .then(stream => {
      ok(stream, 'Got fake stream');
      pcDouble.addStream(stream);
      return pcDouble.createOffer();
    })
    .then(o => {
      offer = o;
      ok(offer, 'Got offer');

      match = offer.sdp.match(fingerprintRegex);
      if (!match) {
        throw new Error('No fingerprint in offer SDP');
      }
      fingerprints = makeFingerprints(match[1], match[2]);
      return getIdentityAssertion(fingerprints);
    })
    .then(assertion => {
      ok(assertion, 'Should have assertion');

      var sdp = offer.sdp.slice(0, match.index) +
          'a=identity:' + assertion + '\n' +
          fingerprintSdp(fingerprints.slice(1)) +
          offer.sdp.slice(match.index);

      var desc = new RTCSessionDescription({ type: 'offer', sdp: sdp });
      return pcStrict.setRemoteDescription(desc);
    })
    .then(() => {
      ok(true, 'Modified fingerprints were accepted');
    }, error => {
      var e = SpecialPowers.wrap(error);
      ok(false, 'error in test: ' +
         (e.message ? (e.message + '\n' + e.stack) : e));
    })
    .then(() => {
      pcStrict.close();
      pcDouble.close();
      SimpleTest.finish();
    });
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({
  set: [ [ 'media.peerconnection.identity.enabled', true ] ]
}, testMultipleFingerprints);
</script>
  </body>
</html>
